package day8.homework;

import java.sql.Array;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//a.一球从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，求它在第10次落地时，共经过多少米？第10次反弹多高？  通过线程模拟这个过程
public
class Demo {
    static int i=0;
    static long start;
    static  long end;
    static int journey1;
    static int journey;
    static boolean b=false;
    public static
    void main(String[] args) throws ExecutionException, InterruptedException {
        demo();
    }
    public static void demo() throws ExecutionException, InterruptedException {
        FutureTask<double[]> ft=new FutureTask<>(()->{
            double[] b1=new double[2];
            b1[0]=100;
            b1[1]=0;
            for (int i=1;i<11;i++){
                if(i<10) {
                    b1[1] += 1.5 * b1[0];
                    b1[0] /= 2;
                }else {
                    b1[1] += 1. * b1[0];
                    b1[0] /= 2;
                }
            }
            return b1;
        });
        new Thread(ft).start();
        double[] b1=ft.get();
        System.out.println("第10次反弹离地："+b1[0]+"米，经过的路程为："+b1[1]+"米");
        Thread t=new Thread(()->{
            double f=100;
            double journey=0;
            for (int i=1;i<11;i++){
                journey+=1.5*f;
                f/=2;
            }
            System.out.println("第"+10+"次反弹离地："+f+"米，经过的路程为："+journey+"米");
        });
        t.start();
    }
    //b.通过线程模拟对同一个变量进行更改,
    //   A线程睡眠10毫秒，对变量加1，B线程睡眠20毫秒对变量加1，持续3秒
    //   查看多次运行的结果是否一致
    public static void demo1(){
        start=System.currentTimeMillis();
        end=start+3000;
        Thread t=new Thread(()-> {
            long l = 0;
            while (l <= end) {
                i++;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                l = System.currentTimeMillis();
            }
            System.out.println(i);
        });
        Thread t1=new Thread(()->{
            long l1=0;
            while (l1<=end) {
                i++;
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
                l1=System.currentTimeMillis();
            }
            System.out.println(i);
        });
            t.start();
            t1.start();
    }
    //a.有三个线程A，B，C，每个线程只打印线程名  保证三个线程的执行顺序永远是 A -->B-->C
    public static void demo2(){
        Thread t1=new Thread(()->{
            System.out.print(Thread.currentThread().getName()+"->");
        },"A");
        Thread t2=new Thread(()->{
            System.out.print(Thread.currentThread().getName()+"->");
        },"B");
        Thread t3=new Thread(()->{
            System.out.print(Thread.currentThread().getName());
        },"C");
        try {
            t1.start();
            t1.join();
            t2.start();
            t2.join();
            t3.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //b.通过多线程模拟两人爬山，每个线程表示一个人， 可以设置爬山速度  每爬完100米显示信息 先到终点的是胜利者， 如果有人已经到达重点了 另一个人则停止爬山
    public static void demo3(){
        int speed=20;
        int speed1=20;
        int high=8848;
        Thread t1=new Thread(()->{
            while (journey<=high){
                if(b) {
                    System.out.println("已有人爬到山顶，"+Thread.currentThread().getName()+"停止爬山");
                    return;
                }
                journey+=speed;
                if(journey%100==0) System.out.println(Thread.currentThread().getName()+"爬到了"+journey+"米");
                if(journey>=high){
                    System.out.println(Thread.currentThread().getName()+"爬到了山顶"+high+"米，胜利了");
                    b=true;
                    return;
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A");

        Thread t2=new Thread(()->{
            while (journey1<=high){
                if(b) {
                    System.out.println("已有人爬到山顶，"+Thread.currentThread().getName()+"停止爬山");
                    return;
                }
                journey1+=speed1;
                if(journey1%100==0){
                    System.out.println(Thread.currentThread().getName()+"爬到了"+journey1+"米");
                }
                if(journey1>=high){
                    System.out.println(Thread.currentThread().getName()+"爬到了山顶"+high+"米，胜利了");
                    b=true;
                    return;
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B");
        System.out.println("开始爬山");
        t1.start();
        t2.start();
    }
}
